﻿// http://www.agstrout.com/?p=6
package rob.ui {
	public class KeyController{
		import flash.ui.Keyboard;
		import flash.display.Stage;
		import flash.events.*;
	
		static private var _instance:KeyController = undefined;
	
		private const UP:uint = 0;
		private const DOWN:uint = 1;
		private const LEFT:uint = 2;
		private const RIGHT:uint = 3;
		private const JUMP:uint = 4;
		private const DUCK:uint = 5;
		private const CAMUP:uint = 6;
		private const CAMDOWN:uint = 7;
		private const CAMLEFT:uint = 8;
		private const CAMRIGHT:uint = 9;
		private const KEYE:uint = 10;
		private const KEYQ:uint = 11;
		
		private var _keys:Array = undefined;
		private var _up:uint = Keyboard.UP;
		private var _down:uint = Keyboard.DOWN;
		private var _left:uint = Keyboard.LEFT;
		private var _right:uint = Keyboard.RIGHT;
		private var _camUp:uint = 87;	// w
		private var _camDown:uint = 83;  // s
		private var _camLeft:uint = 65;  // a
		private var _camRight:uint = 68; // d
		private var _jump:uint = Keyboard.SPACE;
		private var _duck:uint = Keyboard.SHIFT;
		private var _keyQ:uint = 81;
		private var _keyE:uint = 69;
		private var _controls:Array = undefined;
		private var _ocontrols:Array = undefined;
		
		public function KeyController():void{
			_keys = new Array();
			_controls=new Array();
			_ocontrols=new Array();
			var i:Number;
			for(i=0;i<256;i++)_keys[i]=false;
			for(i=0;i<10;i++)_controls[i]=false;
		}
		
		public static function init(stage:Stage):void{
			stage.addEventListener(KeyboardEvent.KEY_DOWN, instance.keyDownHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP, instance.keyUpHandler);
		}
		
		private function keyDownHandler(event:KeyboardEvent):void{
			_keys[event.keyCode]=true;
		}
		
		private function keyUpHandler(event:KeyboardEvent):void{
			_keys[event.keyCode]=false;
		}
		
		private function update():void{
			/*
			_controls[UP]=_keys[_up];
			_controls[DOWN]=_keys[_down];
			_controls[LEFT]=_keys[_left];
			_controls[RIGHT]=_keys[_right];			
			_controls[JUMP]=_keys[_jump];
			_controls[DUCK]=_keys[_duck];
			*/
			_controls[CAMUP]=_keys[_camUp];
			_controls[CAMDOWN]=_keys[_camDown];
			_controls[CAMLEFT]=_keys[_camLeft];
			_controls[CAMRIGHT]=_keys[_camRight];
			_controls[KEYQ]=_keys[_keyQ];
			_controls[KEYE]=_keys[_keyE];
		}
		
		public function tick():void{
			for(var i:Number = 0;i<_controls.length;i++){
				_ocontrols[i]=_controls[i];
			}
			update();
		}
		
		public static function get instance():KeyController{
			if(_instance == null){
				_instance=new KeyController();
			}
			return _instance;
		}
		
		// Legacy style isDown key code check ;)
		public function isDown(n:Number):Boolean{
			return _keys[n];
		}
		
		public function get up():Boolean{return _controls[UP];}
		public function get down():Boolean{return _controls[DOWN];}
		public function get left():Boolean{return _controls[LEFT];}
		public function get right():Boolean{return _controls[RIGHT];}
		
		public function get camUp():Boolean{return _controls[CAMUP];}
		public function get camDown():Boolean{return _controls[CAMDOWN];}
		public function get camLeft():Boolean{return _controls[CAMLEFT];}
		public function get camRight():Boolean{return _controls[CAMRIGHT];}
		
		public function get jump():Boolean{return _controls[JUMP];}
		public function get duck():Boolean{return _controls[DUCK];}
		
		public function get keyE():Boolean{return _controls[KEYE];}
		public function get keyQ():Boolean{return _controls[KEYQ];}
		
		// Setters for the keys
		public function set upKey(n:Number):void{
			_up=n;
		}
		public function set downKey(n:Number):void{
			_down=n;
		}
		public function set leftKey(n:Number):void{
			_left=n;
		}
		public function set rightKey(n:Number):void{
			_right=n;
		}
		public function set jumpKey(n:Number):void{
			_jump=n;
		}
		public function set duckKey(n:Number):void{
			_duck=n;
		}
				
	}
}